package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.handler.JsonResult;
import cn.tedu.ivos.base.pojo.vo.PageData;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQueryParam;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private UserService userService;
    @ApiOperation(value = "用户登录")
    @ApiImplicitParam(name = "userLoginParam",value = "用户登录参数",required = true)
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
       log.debug("userLoginParam:{}",userLoginParam.toString());
       UserVO userVO=userService.login(userLoginParam);
       return new JsonResult(userVO);
    }
    ///......
    @ApiOperation(value = "用户退出")
    @GetMapping("/logout")
    public JsonResult logout(HttpSession session){
        //从session中移除用户信息
        session.removeAttribute("user");
        return JsonResult.ok();
    }

    @ApiOperation("用户列表")
    @GetMapping("/select")
    public JsonResult selectUserList(UserQueryParam userQuery){
        log.debug("查询用户:userQuery={}",userQuery);
        //List<UserVO> list = userService.selectUserList(userQuery);
        PageData<UserVO> userVOPageData = userService.selectUserList(userQuery);
        return JsonResult.ok(userVOPageData);
    }

    @ApiOperation(value = "添加用户")
    @ApiImplicitParam(name = "userSaveParam",value = "用户保存参数",required = true)
    @PostMapping("/save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }
    @ApiOperation(value = "重置密码")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @PutMapping("/update/password/{id}")
    public JsonResult resetPassword(@PathVariable Long id){
        userService.resetPassword(id);
        return JsonResult.ok();
    }
    @ApiOperation(value = "修改状态")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @PutMapping("/update/status/{id}/{status}")
    public JsonResult changeStatus(@PathVariable Long id,@PathVariable String status){
        userService.changePassword(id,status);
        return JsonResult.ok();
    }
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @DeleteMapping("/delete/{id}")
    public JsonResult deleteUser(@PathVariable Long id){
        userService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询用户")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    @GetMapping("/select/{id}")
    public JsonResult selectById(@PathVariable Long id){
       return new JsonResult(userService.selectById(id));
    }



    @ApiOperation("查询审核员")
    @ApiImplicitParam(name = "parentId",value = "审核员id",required = true)
    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable("parentId") Long parentId){
        return new JsonResult(userService.selectAuditList(parentId));
    }

}
